<?php

defined( '_JEXEC' ) or die( 'Direct Access to this location is not allowed.' );


function com_install(){
    $db =& JFactory::getDBO();
	$config = new JConfig();
	$database = $config->db;
	$host = $_SERVER["HTTP_HOST"];
	if($host != "localhost"){
		$sql = "select count(*) from information_schema.statistics where INDEX_NAME='ij_results_a' and INDEX_SCHEMA='".$database."'";
		$db->setQuery($sql);
		$db->query();
		$count = $db->loadResult();
		
		if($count == "0"){
			$sql = "CREATE INDEX ij_results_a on #__ijoomla_surveys_result(a_id)";
			$db->setQuery($sql);
			$db->query();
		}
		
		$sql = "select count(*) from information_schema.statistics where INDEX_NAME='ij_results_text_v' and INDEX_SCHEMA='".$database."'";
		$db->setQuery($sql);
		$db->query();
		$count = $db->loadResult();
		if($count == "0"){
			$sql = "create index ij_results_text_v on #__ijoomla_surveys_result_text(value)";
			$db->setQuery($sql);
			$db->query();
		}
		
		$sql = "select count(*) from information_schema.statistics where INDEX_NAME='ij_results_text_si' and INDEX_SCHEMA='".$database."'";
		$db->setQuery($sql);
		$db->query();
		$count = $db->loadResult();
		if($count == "0"){
			$sql = "create index ij_results_text_si on #__ijoomla_surveys_result_text(session_id)";
			$db->setQuery($sql);
			$db->query();
		}
		
		$sql = "select count(*) from information_schema.statistics where INDEX_NAME='ij_results_text_qi' and INDEX_SCHEMA='".$database."'";
		$db->setQuery($sql);
		$db->query();
		$count = $db->loadResult();
		if($count == "0"){
			$sql = "create index ij_results_text_qi on #__ijoomla_surveys_result_text(question_id)";
			$db->setQuery($sql);
			$db->query();
		}
		
		$sql = "select count(*) from information_schema.statistics where INDEX_NAME='ij_results_session' and INDEX_SCHEMA='".$database."'";
		$db->setQuery($sql);
		$db->query();
		$count = $db->loadResult();
		if($count == "0"){
			$sql = "create index ij_results_session on #__ijoomla_surveys_result(session_id)";
			$db->setQuery($sql);
			$db->query();
		}
		
		$sql = "select count(*) from information_schema.statistics where INDEX_NAME='ij_results_q' and INDEX_SCHEMA='".$database."'";
		$db->setQuery($sql);
		$db->query();
		$count = $db->loadResult();
		if($count == "0"){
			$sql = "create index ij_results_q on #__ijoomla_surveys_result(q_id)";
			$db->setQuery($sql);
			$db->query();
		}
	}
	//install plugins
	$sql = "select element from #__extensions where element='ijoomlanews'";
    $db->setQuery($sql);
	$db->query();
    $name = $db->loadResult();

    if (empty($name)){
       $query = "INSERT INTO #__extensions (name,type,element,folder,client_id,enabled,access,protected,manifest_cache,params,custom_data,system_data,checked_out, 	checked_out_time,ordering,state)"
        ."\n VALUES ('iJoomla News', 'plugin', 'ijoomlanews', 'system', 0, 1, 1, 0, '', '{\"nr_articles\":\"5\",\"text_length\":\"500\",\"image_width\":\"50\"}', '', '', 0, '0000-00-00 00:00:00' , -10300, 0)";
        $db->setQuery($query);
        $db->query();
    }
	
	$sql = "select element from #__extensions where element='ijoomlaupdate'";
    $db->setQuery($sql);
	$db->query();
    $name = $db->loadResult();

    if (empty($name)){
       $query = "INSERT INTO #__extensions (name,type,element,folder,client_id,enabled,access,protected,manifest_cache,params,custom_data,system_data,checked_out, 	checked_out_time,ordering,state)"
        ."\n VALUES ('iJoomla Upgrade Alert', 'plugin', 'ijoomlaupdate', 'system', 0, 1, 1, 0, '', '{\"lastcheck\":\"0\"}', '', '', 0, '0000-00-00 00:00:00' , -10300, 0)";
        $db->setQuery($query);
        $db->query();
    }
	
	$component_dir = JPATH_SITE.'/administrator/components/com_surveys/plugins';
	//----------------------------------------start news plugin
	$news_dir = JPATH_SITE.'/plugins/system/ijoomlanews';
	if(!is_dir($news_dir)){
		mkdir($news_dir, 0755);
	}
	$news_php = 'ijoomlanews.php';
	$news_xml = 'ijoomlanews.xml';
	$news_folder = 'ijoomlanews';
	@chmod($news_dir, 0755);
	if(!copy($component_dir."/ijoomlanews/".$news_xml, $news_dir."/".$news_xml)){
        echo 'Error copying ijoomlanews.xml'."<br/>";
    }	
	if(!copy($component_dir."/ijoomlanews/".$news_php, $news_dir."/".$news_php)){
        echo 'Error copying ijoomlanews.php'."<br/>";
    }	
	if(!is_dir($news_dir."/".$news_folder)){
		mkdir($news_dir."/".$news_folder, 0755);
	}	
	if(!copy($component_dir."/ijoomlanews/".$news_folder."/feed.php", $news_dir."/".$news_folder."/feed.php")){
        echo 'Error copying feed.php'."<br/>";
    }
	if(!copy($component_dir."/ijoomlanews/".$news_folder."/tabs.php", $news_dir."/".$news_folder."/tabs.php")){
        echo 'Error copying tabs.php'."<br/>";
    }
	if(!copy($component_dir."/ijoomlanews/".$news_folder."/index.html", $news_dir."/".$news_folder."/index.html")){
        echo 'Error copying index.html'."<br/>";
    }
	@chmod($component_dir.'/ijoomlanews/'.$news_php, 0755);
    @chmod($component_dir.'/ijoomlanews/'.$news_xml, 0755);
	@chmod($component_dir.'/ijoomlanews/'.$news_folder, 0755);
	if(!unlink($component_dir.'/ijoomlanews/'.$news_php)){
        echo 'Cannot delete '.$component_dir.'/ijoomlanews/'.$news_php."<br/>";
	}
	if(!unlink($component_dir.'/ijoomlanews/'.$news_xml)){
        echo 'Cannot delete '.$component_dir.'/ijoomlanews/'.$news_xml."<br/>";
	}
	//----------------------------------------stop news plugin
	
	//----------------------------------------start upgrade plugin
	$update_dir = JPATH_SITE.'/plugins/system/ijoomlaupdate';
	if(!is_dir($update_dir)){
		mkdir($update_dir, 0755);
	}
	$update_php = 'ijoomlaupdate.php';
	$update_xml = 'ijoomlaupdate.xml';
	$update_folder = 'ijoomlaupdate';
	@chmod($update_dir, 0755);
	if(!copy($component_dir."/ijoomlaupdate/".$update_xml, $update_dir."/".$update_xml)){
        echo 'Error copying ijoomlaupdate.xml'."<br/>";
    }	
	if(!copy($component_dir."/ijoomlaupdate/".$update_php, $update_dir."/".$update_php)){
        echo 'Error copying ijoomlaupdate.php'."<br/>";
    }	
	if(!is_dir($update_dir."/".$update_folder)){
		mkdir($update_dir."/".$update_folder, 0755);
	}	
	if(!copy($component_dir."/ijoomlaupdate/".$update_folder."/editversions.php", $update_dir."/".$update_folder."/editversions.php")){
        echo 'Error copying editversions.php'."<br/>";
    }
	if(!copy($component_dir."/ijoomlaupdate/".$update_folder."/ijoomla.gif", $update_dir."/".$update_folder."/ijoomla.gif")){
        echo 'Error copying ijoomla.gif'."<br/>";
    }
	if(!copy($component_dir."/ijoomlaupdate/".$update_folder."/logo.png", $update_dir."/".$update_folder."/logo.png")){
        echo 'Error copying logo.png'."<br/>";
    }
	if(!copy($component_dir."/ijoomlaupdate/".$update_folder."/index.html", $update_dir."/".$update_folder."/index.html")){
        echo 'Error copying index.html'."<br/>";
    }
	@chmod($component_dir.'/ijoomlaupdate/'.$update_php, 0755);
    @chmod($component_dir.'/ijoomlaupdate/'.$update_xml, 0755);
	@chmod($component_dir.'/ijoomlaupdate/'.$update_folder, 0755);
	if(!unlink($component_dir.'/ijoomlaupdate/'.$update_php)){
        echo 'Cannot delete '.$component_dir.'/ijoomlaupdate/'.$update_php."<br/>";
	}
	if(!unlink($component_dir.'/ijoomlaupdate/'.$update_xml)){
        echo 'Cannot delete '.$component_dir.'/ijoomlaupdate/'.$update_xml."<br/>";
	}
	//----------------------------------------stop upgrade plugin
}

?>